// pages/classify/classify.js

const image = "https://tdesign.gtimg.com/miniprogram/images/example2.png";
const items = new Array(12).fill({ label: "标题文字", image }, 0, 12);

Page({
  // TDESIGN侧边栏组件
  offsetTopList: [],
  data: {
    sideBarIndex: 1,
    categories: [],
    newCategories: [],
  },
  onLoad() {
    this.getCateList();
  },
  onSideBarChange(e) {
    const { value } = e.detail;

    this.setData({ sideBarIndex: value });

    // 根据catid筛选右侧展示宫格数据，存到新数组
    const newCategories = this.data.categories.reduce((obj, item) => {
      if (item.cat_id === value) {
        obj = item;
      }
      return obj;
    });
    this.setData({ newCategories: newCategories.children });
  },

  //   获取分类数据
  async getCateList() {
    const res = await wx.http.get("/api/public/v1/categories");
    const categories = res.data.message;
    this.setData({ categories });
    // 初始化右侧展示宫格数据，存到新数组
    this.setData({newCategories: categories[0].children})
  },
});
